package app.auto.designer.tool;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import app.auto.designer.R;

/**
 * Created by hp on 2018/8/24.
 */

public class LayoutDrawable {
    public static void LayoutToDrawable(View backgroundview, View tosetview, Context ctx) {
        ViewGroup vg = (ViewGroup) LayoutInflater.from(ctx).inflate(R.layout.layout_add_background_layout,null);
        vg.addView(backgroundview);
        vg.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        vg.layout(0, 0, tosetview.getLayoutParams().width, tosetview.getLayoutParams().height);  //根据字符串的长度显示view的宽度
        vg.buildDrawingCache();
        Bitmap bitmap = vg.getDrawingCache();
        Drawable drawable = (Drawable) new BitmapDrawable(bitmap);
        tosetview.setBackgroundDrawable(drawable);

    }
}
